package com.pur.zsms.supplier.service.impl;

import java.math.BigDecimal;
import java.math.MathContext;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.cglib.beans.BeanCopier;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.pur.zsms.matermanage.mater.dao.IMaterDao;
import com.pur.zsms.matermanage.mater.po.MaterLevelBlPo;
import com.pur.zsms.matermanage.mater.po.MaterLevelCblPo;
import com.pur.zsms.matermanage.mater.po.MaterLevelPo;
import com.pur.zsms.matermanage.mater.service.MaterComputeService;
import com.pur.zsms.matermanage.mater.service.impl.MaterComputerServiceImpl;
import com.pur.zsms.matermanage.mater.vo.MaterVo;
import com.pur.zsms.ordercustommanage.dao.ISelectFoodDao;
import com.pur.zsms.ordercustommanage.po.OrderSupplierMaterRealPo;
import com.pur.zsms.ordercustommanage.po.OrderSupplierPo;
import com.pur.zsms.shopmanage.po.ShopPo;
import com.pur.zsms.supplier.dao.SupplierOrderDao;

public class ExcelInMaterValueCostForWeb extends Thread {

	private int supId;
	private JSONObject object;
	private final List<ShopPo> shopPoList;

	private SupplierOrderDao supplierOrderDao;

	private IMaterDao materDaoImpl;

	private ISelectFoodDao selectFoodDaoImpl;

	private MaterComputeService materComputerServiceImpl;

	public ExcelInMaterValueCostForWeb(JSONObject object, List<ShopPo> shopPoList, SupplierOrderDao supplierOrderDao, IMaterDao materDaoImpl,
			ISelectFoodDao selectFoodDaoImpl, MaterComputeService materComputerServiceImpl, int supId) {
		this.object = object;
		this.shopPoList = shopPoList;
		this.supplierOrderDao = supplierOrderDao;
		this.materDaoImpl = materDaoImpl;
		this.selectFoodDaoImpl = selectFoodDaoImpl;
		this.materComputerServiceImpl = materComputerServiceImpl;
		this.supId = supId;
	}

	@Override
	public void run() {
		try {

			int materId = Integer.valueOf(object.get("materId").toString());
			int materLevel = Integer.valueOf(object.get("materLevel").toString());
			String materDay = object.get("date").toString();
			// 进货价格
			BigDecimal materValueUndressedCost = new BigDecimal(object.get("materValueCost").toString());
			// 被分进货数据到材料进货价格备份表
			List<MaterLevelCblPo> materLevelCblPolist = materDaoImpl.getMaterLevelCblPoByMaterIdAndSupIdAndMaterDay(materId, supId, materDay);
			MaterLevelCblPo tMaterLevelCblPo = new MaterLevelCblPo();
			if (materLevelCblPolist != null && materLevelCblPolist.size() > 0) {
				tMaterLevelCblPo = materLevelCblPolist.get(0);
				tMaterLevelCblPo.setMaterValueUndressed(materValueUndressedCost.multiply(new BigDecimal(0.001)));
				tMaterLevelCblPo.setMaterValue(materValueUndressedCost.multiply(new BigDecimal(0.001)));
				materDaoImpl.updateMaterLevelCbl(tMaterLevelCblPo);
			} else {
				tMaterLevelCblPo.setMaterDay(materDay);
				tMaterLevelCblPo.setMaterId(materId);
				tMaterLevelCblPo.setMaterLevel(1);
				tMaterLevelCblPo.setMaterValueUndressed(materValueUndressedCost.multiply(new BigDecimal(0.001)));
				tMaterLevelCblPo.setMaterValue(materValueUndressedCost.multiply(new BigDecimal(0.001)));
				tMaterLevelCblPo.setSupId(supId);
				materDaoImpl.addMaterLevelCbl(tMaterLevelCblPo);
			}
			// 卖出价格Start
			for (ShopPo shopPo : shopPoList) {

				// 校验用户是否订单日 订购此种材料
				List<OrderSupplierPo> orderSupplierPolist = supplierOrderDao.isExistOrderSup(String.valueOf(shopPo.getShopId()),
						String.valueOf(supId), materDay, materId);
				if (orderSupplierPolist == null || orderSupplierPolist.size() == 0) {
					continue;
				}
				BigDecimal materValueUndressed = materComputerServiceImpl.ComputeMaterRaise(materValueUndressedCost, materId, supId,
						shopPo.getShopId());
				/*MaterLevelPo materLevelPo = new MaterLevelPo();

				// 计算净料价格
				MathContext mc = new MathContext(5, RoundingMode.HALF_UP);
				BigDecimal materValueNet = null;

				List<MaterLevelPo> materLevelPoList = materDaoImpl.getMaterLevelPoByMaterIdAndLevelAndSupId(materId, materLevel, supId,
						String.valueOf(shopPo.getShopId()));
				if (materLevelPoList == null || materLevelPoList.size() == 0) {
					MaterLevelPo materLevelPoOther = materDaoImpl.getMaterLevelPoByMaterId(materId).get(0);

					BeanCopier copier = BeanCopier.create(MaterLevelPo.class, MaterLevelPo.class, false);
					copier.copy(materLevelPoOther, materLevelPo, null);
					materLevelPo.setMid(0);
					materLevelPo.setSupId(supId);
					materLevelPo.setCustId((int) shopPo.getShopId());

					// 1替换当前材料表中 的价格
					materValueNet = materValueUndressed.multiply(new BigDecimal(0.1)).divide(new BigDecimal(materLevelPo.getMaterProportion()), mc)
							.setScale(5, BigDecimal.ROUND_HALF_UP);
					// 改变po
					materLevelPo.setMaterValue(materValueNet);
					materLevelPo.setMaterValueUndressed(materValueUndressed.multiply(new BigDecimal(0.001)));

					materDaoImpl.addMaterLevelPersist(materLevelPo);
				} else {
					materLevelPo = materLevelPoList.get(0);
					// 1替换当前材料表中 的价格
					materValueNet = materValueUndressed.multiply(new BigDecimal(0.1)).divide(new BigDecimal(materLevelPo.getMaterProportion()), mc)
							.setScale(5, BigDecimal.ROUND_HALF_UP);
					// 改变po
					materLevelPo.setMaterValue(materValueNet);
					materLevelPo.setMaterValueUndressed(materValueUndressed.multiply(new BigDecimal(0.001)));
					materDaoImpl.addMaterLevel(materLevelPo);
				}

				// 2.插入材料备份表中
				List<MaterLevelBlPo> materLevelBlPoList = materDaoImpl.getMaterLevelBlPoByMaterIdAndLevelAndSupId(materId, materLevel, supId,
						materDay, String.valueOf(shopPo.getShopId()));
				MaterLevelBlPo materLevelBlPo = new MaterLevelBlPo();
				if (materLevelBlPoList != null && materLevelBlPoList.size() > 0) {
					materLevelBlPo = materLevelBlPoList.get(0);
					BeanCopier copier = BeanCopier.create(MaterLevelPo.class, MaterLevelBlPo.class, false);
					copier.copy(materLevelPo, materLevelBlPo, null);
					materLevelBlPo.setMaterDay(materDay);
					materLevelBlPo.setMaterValue(materValueNet);
					materLevelBlPo.setMaterValueUndressed(materValueUndressed.multiply(new BigDecimal(0.001)));
					materDaoImpl.updateMaterLevelBl(materLevelBlPo);
				} else {
					BeanCopier copier = BeanCopier.create(MaterLevelPo.class, MaterLevelBlPo.class, false);
					copier.copy(materLevelPo, materLevelBlPo, null);
					materLevelBlPo.setMaterDay(materDay);
					materLevelBlPo.setMaterValue(materValueNet);
					materLevelBlPo.setMaterValueUndressed(materValueUndressed.multiply(new BigDecimal(0.001)));
					materDaoImpl.addMaterLevelBl(materLevelBlPo);
				}*/

				// 改变所提交日期的实际价格 并生成总价(改变未送货的送货单)
				List<OrderSupplierMaterRealPo> tOrderSupMaterRealPoList = supplierOrderDao.getOrderSupMaterRealListBySupIdOrdDateMaterIdAndCustId(
						supId, materDay, materId, String.valueOf(shopPo.getShopId()));

				if (tOrderSupMaterRealPoList != null && tOrderSupMaterRealPoList.size() > 0) {
					for (OrderSupplierMaterRealPo orderSupplierMaterRealPo : tOrderSupMaterRealPoList) {
						orderSupplierMaterRealPo.setOrdMaterValueUnit(materValueUndressed);
						orderSupplierMaterRealPo.setOrdMaterValue(materValueUndressed.multiply(orderSupplierMaterRealPo.getOrdMaterWeight()));
						//selectFoodDaoImpl.updateOrderSupplierMaterReal(orderSupplierMaterRealPo);

						orderSupplierMaterRealPo.setOrdMaterValueCostUnit(materValueUndressedCost.toString());
						orderSupplierMaterRealPo.setOrdMaterValueCost(materValueUndressedCost.multiply(orderSupplierMaterRealPo.getOrdMaterWeight())
								.toString());
						supplierOrderDao.updateOrderSupplierMaterReal(orderSupplierMaterRealPo);
					}
				}
				
				
				
				// 改变所提交日期的实际价格 并生成总价(改变送货后的送货单)
				List<OrderSupplierMaterRealPo> tOrderSupMaterRealPoListNotPrt = supplierOrderDao.getOrderSupMaterRealListBySupIdOrdDateMaterIdAndCustIdForNotPrt(
						supId, materDay, materId, String.valueOf(shopPo.getShopId()));

				if (tOrderSupMaterRealPoListNotPrt != null && tOrderSupMaterRealPoListNotPrt.size() > 0) {
					for (OrderSupplierMaterRealPo orderSupplierMaterRealPo : tOrderSupMaterRealPoListNotPrt) {
						//selectFoodDaoImpl.updateOrderSupplierMaterReal(orderSupplierMaterRealPo);

						orderSupplierMaterRealPo.setOrdMaterValueCostUnit(materValueUndressedCost.toString());
						orderSupplierMaterRealPo.setOrdMaterValueCost(materValueUndressedCost.multiply(orderSupplierMaterRealPo.getOrdMaterWeight())
								.toString());
						supplierOrderDao.updateOrderSupplierMaterReal(orderSupplierMaterRealPo);
					}
				}
			}
			// 卖出价格End

		} catch (Exception e) {
			e.printStackTrace();
		}// finally {
			// System.out.println("子线程结束");
		// }

	}
}